package com.itboye.lottery.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.bylibrary.util.TimeUtils;
import com.itboye.bylibrary.widget.RatioImageView;
import com.itboye.bylibrary.widget.glide.GlideUtil;
import com.itboye.lottery.R;
import com.itboye.lottery.bean.FindBean;

import java.util.ArrayList;

/**
 * Created by wu on 2018/1/9.
 */

public class FindChildAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    ArrayList<FindBean> classEntityArrayList = new ArrayList<>();
    Activity activity;
    private int typePosition = 0;

    public FindChildAdapter(Activity activity, ArrayList<FindBean> classEntityArrayList) {
        this.classEntityArrayList = classEntityArrayList;
        this.activity = activity;
    }

    public OnItemClickListener itemClickListener;

    public void setOnItemClickListener(FindChildAdapter.OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public void setTypePosition(int position) {
        this.typePosition = position;
    }


    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        int view = 0;
        switch (typePosition) {
            case 0:
            case 2:
                view = R.layout.item_wanfa;
                break;
            case 1:
                view = R.layout.item_news;
                break;
            case 3:
                view = R.layout.item_notice_center;
                break;
        }
        return new ViewHolderFind(LayoutInflater.from(context).inflate(view, parent, false));
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        FindBean findBean = classEntityArrayList.get(position);
        ViewHolderFind viewHolder = ((ViewHolderFind) holder);
        switch (typePosition) {
            case 0:
                viewHolder.item_wanfa_tv_title.setText(findBean.getName());
                viewHolder.item_wanfa_tv_chakan.setText("点击查看");
                GlideUtil.display(context, findBean.getImg(), viewHolder.item_wanfa_iv_icon,R.mipmap.banner_default,false);
                break;
            case 2:
                viewHolder.item_wanfa_tv_title.setText(findBean.getTitle());
                viewHolder.item_wanfa_tv_chakan.setText("点击查看");
                GlideUtil.display(context, findBean.getImg(), viewHolder.item_wanfa_iv_icon,R.mipmap.banner_default);
                break;
            case 1:
                viewHolder.item_tv_news_title.setText(findBean.getTitle());
                viewHolder.item_tv_news_content.setText(TimeUtils.getFormatTime(findBean.getAdd_time() * 1000, "yyyy-MM-dd   HH:mm"));
                viewHolder.item_tv_news_read_num.setText("阅读" + findBean.getClick());
                GlideUtil.display(context, findBean.getImg(), viewHolder.iv_fragment_news_icon,R.mipmap.lottery_circle);
                break;
            case 3:
                viewHolder.iv_fragment_noticeCenter_icon.setImageResource(R.mipmap.yuandian);
                viewHolder.item_tv_news_title.setText(findBean.getTitle());
                viewHolder.item_tv_news_content.setText(TimeUtils.getFormatTime(findBean.getAdd_time() * 1000, "yyyy-MM-dd   HH:mm"));
                break;
        }

//        GlideUtil.display(context, list.get(position).getImg_url(), holder.img);
//        holder.name.setText(list.get(position).getClassification_name());
    }

    @Override
    public int getItemCount() {
        return classEntityArrayList == null ? 0 : classEntityArrayList.size();
    }

    public class ViewHolderFind extends RecyclerView.ViewHolder implements View.OnClickListener {
        public RatioImageView item_wanfa_iv_icon;
        public TextView item_wanfa_tv_title, item_wanfa_tv_chakan;
        //        public TextView content;
        TextView item_tv_news_title, item_tv_news_content, item_tv_news_read_num;
        RatioImageView iv_fragment_news_icon;
        ImageView iv_fragment_noticeCenter_icon;

        public ViewHolderFind(View itemView) {
            super(itemView);
            item_wanfa_iv_icon = itemView.findViewById(R.id.item_wanfa_iv_icon);
            item_wanfa_tv_title = itemView.findViewById(R.id.item_wanfa_tv_title);
            item_wanfa_tv_chakan = itemView.findViewById(R.id.item_wanfa_tv_chakan);


            item_tv_news_title = itemView.findViewById(R.id.item_tv_news_title);
            item_tv_news_content = itemView.findViewById(R.id.item_tv_news_content);
            try {
                item_tv_news_read_num = itemView.findViewById(R.id.item_tv_news_read_num);
            } catch (Exception e) {

            }
            iv_fragment_news_icon = itemView.findViewById(R.id.iv_fragment_news_icon);
            iv_fragment_noticeCenter_icon = itemView.findViewById(R.id.iv_fragment_noticeCenter_icon);
//            content = itemView.findViewById(R.id.item_tv_find_content);
//
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (itemClickListener != null) {
                itemClickListener.onItemClick(v, getAdapterPosition());
            }
        }
    }

}
